<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\OrderDetail;
use app\common\business\Seat as BusObj;
use app\common\business\SeatDetail;
use app\common\business\SeatDialog;
use app\common\lib\Color;
use app\common\lib\Radom;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;
use think\facade\Log;
use think\facade\View;

class Seat extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    public function index()
    {
        $this->indexField = "*,venue as get_venue,theatre_cate_id as get_theatre_cate_id";
        return parent::index(); // TODO: Change the autogenerated stub
    }

    public function dialog()
    {
        $id = $this->request->param('id', 0, 'intval');
//        dump($id);exit();
        //根据id查询座位
        try {
            $detail = (new SeatDetail())->getSeatDetailByList($id);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        $seatDetail = Show::getArraySeat($detail);
        $lists = Show::getArrayList($detail);
        return $this->fetch('', [
            "id" => $id,
            "seatDetail" => $seatDetail,
            "all" => Show::json_encode($lists['all'] ?? []),
//            "dis" => Show::json_encode($lists['dis'] ?? []),
        ]);
    }

    public function submit()
    {
        $all = $this->request->param('all', '', 'trim');
        $disabled = $this->request->param('disabled', '', 'trim');
        $seat_id = $this->request->param('seat_id', 0, 'intval');
        if ($seat_id == 0) {
            return Show::json_show(Status::status_error, '请关闭重试');
        }
        $data = [
            "all" => $all,
            "disabled" => $disabled
        ];
        try {
            $result = (new SeatDetail())->addSeatDetail($data, $seat_id);
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            return Show::json_show(Status::status_error, "添加发生错误！请重试");
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "保存成功");
        } else {
            return Show::json_show(Status::status_error, "保存失败");
        }
    }

    public function getSeatByList()
    {
        $cate_id = $this->request->param('cate_id', 0, 'intval');
        if ($cate_id == 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        try {
            $lists = $this->busObj->getSeatByList($cate_id, "id as value,title as name");
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        return Show::json_show(Status::status_normal, "ok", $lists);
    }

    /**
     * @throws Exception
     */
    public function setDialog()
    {
        //先查询座位
        $sid = $this->request->param('sid', 0, "intval");
        if ($sid == 0) {
            throw new Exception("类型错误");
        }
        try {
            $seat = $this->busObj->getBaseById($sid);
        } catch (\Exception $e) {
            throw new Exception("内部错误");
        }
        if (!$seat) {
            throw new Exception("内部错误");
        }
        try {
            $seatDetail = (new SeatDetail())->getSeatDetailByList($sid);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }

        //先把数据插入数据库
        //查询是否存在id

        if (empty($seatDetail)) {
            try {
                $dialog = (new SeatDialog())->getSeatDialogByTypeList($seat['type'], 'YCoord,XCoord,RowNum,SeatCode,Status,ColumnNum');
            } catch (\Exception $e) {
                throw new Exception($e->getMessage());
            }
            $insertData = [];
            foreach ($dialog as $key => $value) {
                $insertData[$key] = $value;
                $insertData[$key]['seat_id'] = $sid;
                $insertData[$key]['SeatCode'] = $sid . $value['SeatCode'];

            }
//            dump($insertData);exit();
            try {
                $res = (new SeatDetail())->addSaveAll($insertData);
            } catch (\Exception $e) {
                throw new Exception("操作失败");
            }
        }
        $seat_list = [];

        $seat_detail = (new SeatDetail())->getSeatDetailByList($sid);
        //dump($seat_detail);exit();
        //查询座位 且座位不是禁用状态的
        $normal_seat = (new SeatDetail())->getSeatDetailByListStatusNoraml($sid);
        if (!empty($normal_seat)) {
            //获取编号
            //dump(json_encode($normal_seat));exit();
            $seatId = array_column($normal_seat, 'SeatCode');

            $orderList = (new OrderDetail())->getOrderDetailSeatIdByList($seatId);
            //dump($orderList);exit();
            if (!empty($orderList)) {
                $orderLists = array_column($orderList, 'seat_id');
                //dump($orderLists);exit();
                foreach ($seat_detail as $key => $value) {
                    if (in_array($value['SeatCode'], $orderLists)) {
                        $seat_detail[$key]['Status'] = 2;
                    }
                }
            }
        }
        //dump($seat_detail);exit();
        //循环座位
        foreach ($seat_detail as $v) {
            if (isset($seat_list[$v['YCoord']])) {
                array_push($seat_list[$v['YCoord']], $v);
            } else {
                $seat_list[$v['YCoord']] = [$v];
            }
        }

        //查询价格不同
        $money_list = (new SeatDetail())->getSeatDetailByMoneyList($sid);
        $money = array_column($money_list, 'money');
        $color_money = [];
        foreach ($money as $value) {
            $color_money[$value] = [
                "color" => Color::random_color(),
                "money" => $value
            ];
        }
        View::assign('money', $color_money);
        //dump($seat_list);exit();
        $seat_list_data = [];
        $listData = [];
        foreach ($seat_list as $key => $value) {
            foreach ($value as $k => $v) {
                $v['color'] = "#" . $color_money[$v['money']]['color'];
                $seat_list_data[$k] = $v;
            }
            $listData[$key] = $seat_list_data;
        }
//        dump($listData);exit();
        View::assign('seat', $listData);
        View::assign('sid', $sid);
        return View::fetch();
    }

    public function dialogOpen()
    {
        $data = $this->request->param('data', '', 'trim');
        $sid = $this->request->param('sid', 0, 'intval');
        if (empty($data)) {
            throw new Exception("请重试");
        }
        if ($sid == 0) {
            throw new Exception("内部错误");
        }
        return View::fetch('', [
            'seat' => $data,
            'sid' => $sid
        ]);
    }

    public function saveSeatOpen()
    {
        $money = $this->request->param('money', '', 'trim');
        $seat = $this->request->param('seat', '', 'trim');
        $switch = $this->request->param('switch', 0, 'intval');
        $sid = $this->request->param('sid', 0, 'intval');
        $seat_id = explode(',', $seat);
        $lists = [];
        if ($switch == 1) {
            $status = 0;
        } else {
            $status = 2;
        }
        foreach ($seat_id as $value) {
            $v = explode("-", $value);
            $lists[] = [
                "RowNum" => $v[0],
                "ColumnNum" => $v[1],
                "money" => $money,
                "Status" => $status,
            ];
        }
        try {
            (new SeatDetail)->setSeatDetailByInfo($lists, $sid);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        return Show::json_show(Status::status_normal, "成功");
    }
}
